<template>
  <div class="container">
    <!-- 面包屑 -->
    <div class="bread-container">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item
          :to="{ path: `/category/${subCategoryData.parentId}` }"
          >{{ subCategoryData.parentName }}</el-breadcrumb-item
        >
        <el-breadcrumb-item>{{ subCategoryData.name }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="sub-container">
      <el-tabs
        type="card"
        v-model="goodsParams.sortField"
        @tab-change="tabChange"
      >
        <el-tab-pane label="最新商品" name="publishTime"></el-tab-pane>
        <el-tab-pane label="最高人气" name="orderNum"></el-tab-pane>
        <el-tab-pane label="评论最多" name="evaluateNum"></el-tab-pane>
      </el-tabs>
      <div
        class="body"
        v-infinite-scroll="load"
        :infinite-scroll-disabled="disabled"
      >
        <!-- 商品列表 -->
        <GoodsItem
          v-for="good in goodsList"
          :key="GoodsItem.id"
          :good="good"
        ></GoodsItem>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineOptions } from "vue";
//引入公共组件
import GoodsItem from "../Home/components/GoodsItem.vue";
//引入二级分类商品列表数据的API
import { reqSubGoodList } from "@/apis/category";
defineOptions({
  name: "SubCategory",
});
//引入自定义hook函数钩子
import { useBreadecrumb } from "./composables/useBreadcrumb";
import { useGoodList } from "./composables/useGoodList";
//解构数据
const { subCategoryData } = useBreadecrumb();
const { goodsList, goodsParams, getGoodsList, disabled } = useGoodList();
//点击tab更换数据切换
const tabChange = () => {
  console.log("tab改变了", goodsParams.value.sortField);
  getGoodsList();
};
//列表无限下载功能实现的回调（搭配element-UI：v-infinite-scroll）
const load = async () => {
  console.log("数据到底了");
  //请求下一页
  goodsParams.value.page++;
  //重新获取数据
  getGoodsList(goodsParams.value);
};
</script>

<style scoped lang="scss">
.bread-container {
  padding: 25px 0;
  color: #666;
}
.sub-container {
  padding: 20px 10px;
  background-color: #fff;
  .body {
    display: flex;
    flex-wrap: wrap;
    padding: 0 10px;
    justify-content: space-between;
  }
}
</style>
